#!/bin/env bash

##################################################################################
# 测试平台信息，该信息用于确定QE定标数据
# OP: 可见光
# UV: 近紫外
# IR: 近红外
TEST_PLATFORM="OP"

# 指定探测器型号
export LABTEST_CAMTYPE="gm1201a"
#export LABTEST_CAMTYPE="e2v290"

# 指定跟读出电路版本
# 使用直流版时设为1, 4615 rows
# 使用交流版时设为0, 4616 rows
export READOUR_IS_DC=0

# 指定主温度
export SYSTEMP=188
# 指定暗场温度采样间隔
export DELTA_TEMP=5

# 暗场统计是否排除温像元？
export WARM_PIXEL_DC=0

# 数据处理日期
DATE="2025-10-17"

# 指定定标数据所在目录
# CALPATH="/home/csst/program/detector_pipeline/calibration/"
CALPATH="/home/wangbo/naoc/ccd-data-analyz/calibration/"

# 指定QE定标探头数据
QE_CAL_MONITOR_DB=${CALPATH}/monitor/2024/db_S2281_01_1B007_20240511.tab
QE_CAL_CALIB_DB=${CALPATH}/monitor/2024/db_S2281_6K024_20240708.tab
##################################################################################

# 指定测试数据目录
DATA_PATH="data"   #数据主目录，默认为data/

PTC_PATH=${DATA_PATH}/ptc_20251016_625nm
DARK_PATH=${DATA_PATH}/dark_20251015
QE_CAL_PATH=${DATA_PATH}/qe_calib_20251002T205636
QE_PATH=${DATA_PATH}/qe_20251014_rsp
FLAT_PATH=${DATA_PATH}/flat_20251014
ETRANS_PATH=${DATA_PATH}/etrans_20251013
CROSS_PATH=${DATA_PATH}/crosstalk_20250326
PERSIST_PATH=${DATA_PATH}/persist_20250326
#SCAN_PATH=${DATA_PATH}/scan_20241203_a
SCAN_PATH=${DATA_PATH}/scan_20250326_b

# 指定输出结果目录
RES_PATH="processed_${DATE}"

RES_PTC_PATH=${RES_PATH}/ptc_${DATE}
RES_DARK_PATH=${RES_PATH}/dark_${DATE}
RES_QE_PATH=${RES_PATH}/qe_${DATE}
RES_QE_CAL_PATH=${DATA_PATH}/qe_calib_${DATE}
RES_FLAT_UV_PATH=${RES_PATH}/flat_uv_${DATE}
RES_FLAT_OP_PATH=${RES_PATH}/flat_op_${DATE}
RES_FLAT_IR_PATH=${RES_PATH}/flat_ir_${DATE}
RES_ETRANS_PATH=${RES_PATH}/etrans_${DATE}
RES_CROSS_PATH=${RES_PATH}/crosstalk_${DATE}
RES_PERSIST_PATH=${RES_PATH}/persist_${DATE}
RES_SCAN_PATH=${RES_PATH}/scan_b
#RES_SCAN_PATH=${RES_PATH}/scan_b
# 指定该芯片的增益数据fits
GAINMAP="${RES_PTC_PATH}/data/gain.fits"

# 指定MTF标定值
MTF_CAL=0.73

# 指定是否进行统计类项目的处理
RUN_PTC=T
RUN_DARK=F
RUN_FLAT=F
RUN_ETRANS=F
RUN_QE_CAL=F
RUN_QE=F
RUN_STATS=F
RUN_CROSS=F
RUN_PERSIST=F
RUN_SCAN=F

##################################################################################
# 格式化监视探头数据
format_monitor.py ${DATA_PATH}
switch_time_form.py ${QE_CAL_PATH}/switchtime.txt 

#################################
# 处理PTC
if [ ${RUN_PTC} == "T" ]; then
    echo "**** processing PTC ****"
    run_ptc.py ${PTC_PATH} ${RES_PTC_PATH} 650
#run_ptc.py data/ptc_20221108 processed/ptc_opt -cl True
#run_ptc.py data/ptc_20221108 processed/ptc_inter -cl True -f False
fi
# 对于F24的PTC而言，需要额外做拟合，即对残差进行拟合，之后再重新确定满阱
# cd processed/ptc_20230413;
# residual_linear_fit_dir.py data/ nl_fit 850

#################################
# 暗场
# 数据处理之前需要确认imgconfig中的温度设置正确
if [ ${RUN_DARK} == "T" ]; then
    echo "**** processing Dark ****"
    run_image.py ${DARK_PATH} ${RES_DARK_PATH}
    proc_dark.py ${RES_DARK_PATH} -g $GAINMAP
fi


#################################
# QE

#################################
# QE carlibration
# QE数据中的'qe-0.0-1.fits'需要删掉
if [ ${RUN_QE_CAL} == "T" ]; then
    run_qe_calib.py  ${QE_CAL_PATH} ${RES_QE_CAL_PATH} -c ${QE_CAL_CALIB_DB} -m ${QE_CAL_MONITOR_DB}
fi

# QE定标数据需要设置为对应测试平台的定标数据
# <<<<< 注意选择正确的定标数据>>>>>
QE_CAL_DATA=''
if [ ${TEST_PLATFORM} == "OP" ]; then
    QE_CAL_DATA=${CALPATH}/qe/qe_calib_op/qe_calib_sphere.tab
elif [ ${TEST_PLATFORM} == "UV" ]; then
    QE_CAL_DATA=${CALPATH}/qe/qe_calib_uv/qe_calib_sphere.tab
elif [ ${TEST_PLATFORM} == "IR" ]; then
    QE_CAL_DATA=${CALPATH}/qe/qe_calib_ir/qe_calib_sphere.tab
fi

# 也可以单独指定处理好的定标数据
QE_CAL_DATA=${RES_QE_CAL_PATH}/qe_calib_sphere.tab

if [ ${RUN_QE} == "T" ]; then
    echo "**** processing QE ****"
    run_image.py ${QE_PATH} ${RES_QE_PATH}
    proc_qe.py ${RES_QE_PATH} \
        -g ${GAINMAP} \
        -a ${QE_CAL_DATA} \
        -s ${CALPATH}/glass/3mm.tab
fi

#################################
# PRNU
if [ ${RUN_FLAT} == "T" ]; then
    echo "**** processing flat ****"
    
    if [ ${TEST_PLATFORM} == "UV" ]; then
        run_image.py ${FLAT_PATH} ${RES_FLAT_UV_PATH}
        run_image.py ${FLAT_PATH} ${RES_FLAT_OP_PATH}
        proc_flat.py ${RES_FLAT_UV_PATH} -g $GAINMAP -b uv
        proc_flat.py ${RES_FLAT_OP_PATH} -g $GAINMAP -b op
    fi
    
    if [ ${TEST_PLATFORM} == "OP" ]; then
        run_image.py ${FLAT_PATH} ${RES_FLAT_OP_PATH}
        proc_flat.py ${RES_FLAT_OP_PATH} -g $GAINMAP -b op
    fi
    
    if [ ${TEST_PLATFORM} == "IR" ]; then
        run_image.py ${FLAT_PATH} ${RES_FLAT_IR_PATH}
        run_image.py ${FLAT_PATH} ${RES_FLAT_OP_PATH}
        proc_flat.py ${RES_FLAT_IR_PATH} -g $GAINMAP -b ir
        proc_flat.py ${RES_FLAT_OP_PATH} -g $GAINMAP -b op
    fi
fi

#################################
# CTE
if [ ${RUN_ETRANS} == "T" ]; then
    echo "**** processing etrans ****"
    run_image.py ${ETRANS_PATH} ${RES_ETRANS_PATH} -x
    proc_etrans.py ${RES_ETRANS_PATH} -g ${RES_PTC_PATH}/data/gain.tab
fi

#################################
# stats
# 当前版本中需要修改proc_mask.py和proc_stats.py中的部分代码
if [ ${RUN_STATS} == "T" ]; then
    echo "**** generating mask ****"
#    proc_mask.py ${RES_PATH} -o ${RES_PATH}/mask
#    proc_stats.py ${RES_PATH} -m ${RES_PATH}/mask/mask.fits -g $GAINMAP

    echo "**** ${RES_DARK_PATH}/data"
    echo "**** ${RES_FLAT_PATH}/data"
    echo "**** ${RES_ETRANS_PATH}/data"
    
    if [ ${TEST_PLATFORM} == "UV" ]; then
        proc_mask.py ${RES_PATH} -o ${RES_PATH}/mask \
            --darkmask_data ${RES_DARK_PATH}/data \
            --flatmask_data ${RES_FLAT_UV_PATH}/data \
            --trapmask_data ${RES_ETRANS_PATH} \
            --trap_tab ${RES_ETRANS_PATH}
        proc_stats.py ${RES_PATH} -m ${RES_PATH}/mask/mask.fits -g $GAINMAP \
            --bias_data ${RES_DARK_PATH}/data \
            --dark_data ${RES_DARK_PATH}/data \
            --flat_data ${RES_FLAT_UV_PATH}/data
        proc_stats.py ${RES_PATH} -m ${RES_PATH}/mask/mask.fits -g $GAINMAP \
            --bias_data ${RES_DARK_PATH}/data \
            --dark_data ${RES_DARK_PATH}/data \
            --flat_data ${RES_FLAT_OP_PATH}/data
    elif [ ${TEST_PLATFORM} == "IR" ]; then
        proc_mask.py ${RES_PATH} -o ${RES_PATH}/mask \
            --darkmask_data ${RES_DARK_PATH}/data \
            --flatmask_data ${RES_FLAT_IR_PATH}/data \
            --trapmask_data ${RES_ETRANS_PATH} \
            --trap_tab ${RES_ETRANS_PATH}
        proc_stats.py ${RES_PATH} -m ${RES_PATH}/mask/mask.fits -g $GAINMAP \
            --bias_data ${RES_DARK_PATH}/data \
            --dark_data ${RES_DARK_PATH}/data \
            --flat_data ${RES_FLAT_IR_PATH}/data
        proc_stats.py ${RES_PATH} -m ${RES_PATH}/mask/mask.fits -g $GAINMAP \
            --bias_data ${RES_DARK_PATH}/data \
            --dark_data ${RES_DARK_PATH}/data \
            --flat_data ${RES_FLAT_OP_PATH}/data
    else
        proc_mask.py ${RES_PATH} -o ${RES_PATH}/mask \
            --darkmask_data ${RES_DARK_PATH}/data \
            --flatmask_data ${RES_FLAT_OP_PATH}/data \
            --trapmask_data ${RES_ETRANS_PATH} \
            --trap_tab ${RES_ETRANS_PATH}
        proc_stats.py ${RES_PATH} -m ${RES_PATH}/mask/mask.fits -g $GAINMAP \
            --bias_data ${RES_DARK_PATH}/data \
            --dark_data ${RES_DARK_PATH}/data \
            --flat_data ${RES_FLAT_OP_PATH}/data
    fi
fi

#################################
# crosstalk
# 首先对图像做预处理
# bad.fits是可选项
if [ ${RUN_CROSS} == "T" ]; then
    run_image.py ${CROSS_PATH} ${RES_CROSS_PATH}
    proc_cross.py ${RES_CROSS_PATH} -g $GAINMAP -m ${RES_PATH}/mask/bad.fits
    plot_cross.py ${RES_CROSS_PATH}/crosstalk_result.tab
fi

#################################
# persist
# 残像的处理需要一张对应温度的处理后的主本底，另外需要一个标记缺陷像元的mask
if [ ${RUN_PERSIST} == "T" ]; then
    run_image.py ${PERSIST_PATH} ${RES_PERSIST_PATH} -b ${RES_DARK_PATH}/data/bias_188k.fits
    proc_persist.py ${RES_PERSIST_PATH} -g $GAINMAP -m ${RES_PATH}/mask/bad.fits
fi

#################################
# spot scan
# 邵立的原版程序处理之后再用filter.py再次滤波
if [ ${RUN_SCAN} == "T" ]; then
    run_image.py ${SCAN_PATH} ${RES_SCAN_PATH} -c e2v290_scan -he 100
    proc_scan.py ${RES_SCAN_PATH}
    filter.py ${RES_SCAN_PATH}/data/scan_*_data.dat -o ${RES_SCAN_PATH}
fi

#################################
# cosmic ray
# 利用宇宙线计算电荷扩散
# PAR="dark_188k_20250325T194039-s001.fits,6243,6425,18,33"
# PAR="$PAR;dark_188k_20250325T194039-s001.fits,6842,6311,12,23"
# PAR="$PAR;dark_188k_20250325T194039-s001.fits,7628,5465,16,29"
# PAR="$PAR;dark_188k_20250325T194039-s001.fits,7699,3927,18,12"
# proc_cr.py processed_2025-03-27/dark_2025-03-27 processed_2025-03-27/cosmicray_2025-03-27 -p $PAR 

#################################
# MTF
# 先预处理，需要指定fits图像的行数
# run_image.py data/mtf_20230415_a processed/mtf_20230415_a -c e2v290_scan -he 400

# 低电子、高电子图像各取至少横、竖各一边，共计至少4各边
#PAR="20230322T144347,5194,70,29,32"
#PAR="$PAR;20230322T144347,5170,94,31,29"
#PAR="$PAR;20230322T145505,5192,71,22,26"
#PAR="$PAR;20230322T145505,5173,91,27,21"
#PAR="20230313T133034,6472,123,38,33"
#PAR="$PAR;20230313T134044,6472,123,38,33"
#PAR="20230313T133034,6472,123,38,33"
#PAR="$PAR;20230313T134044,6472,123,38,33"
#proc_mtf.py processed/mtf_0322_c -p $PAR -m ${MTF_CAL}






